package org.mech.sefer.client.ui.component.bibletreeview;

import java.util.List;

import org.apache.pivot.wtk.TreeView;
import org.apache.pivot.wtk.content.TreeBranch;
import org.apache.pivot.wtk.content.TreeNode;
import org.apache.pivot.wtk.content.TreeViewNodeRenderer;
import org.mech.sefer.client.i18n.I18n;
import org.mech.sefer.client.i18n.I18nAware;
import org.mech.sefer.client.ui.ResourcesConstants;
import org.mech.sefer.model.bible.TBible;
import org.mech.sefer.model.bible.TBook;
import org.mech.sefer.shared.common.CollectionUtils;

/**
 * <p>
 * The BibleTreeView is Bible tree view
 * </p>
 * <p>
 * Date: 25.4.2012 13:24:12
 * </p>
 * 
 * @author martin.krajc
 */
public class BibleTree extends TreeView implements I18nAware {

	/**
	 * <p>
	 * The Mode of bible view
	 * </p>
	 * <p>
	 * Date: 26.4.2012 10:44:24
	 * </p>
	 * 
	 * @author martin.krajc
	 */
	public enum Mode {

		/** The LIST mode - all books in bible are display under one branch */
		LIST,

		/** The OLD_NEW books are divided into old and new testament */
		OLD_NEW
	}

	/** The bible. */
	private List<TBible> bibles;

	/** The mode. */
	private Mode mode;

	private TreeBranch oldBranch;
	private TreeBranch newBranch;

	private String oldBLabel;

	private String newBLabel;
	
	/**
	 * Instantiates a new bible tree view.
	 */
	public BibleTree() {
		((TreeViewNodeRenderer) getNodeRenderer()).setIconHeight(24);
		((TreeViewNodeRenderer) getNodeRenderer()).setIconWidth(24);
		mode = Mode.LIST;
	}

	/**
	 * Refresh the view
	 */
	private void refresh() {
		TreeBranch root = new TreeBranch();

		if (CollectionUtils.isNotEmpty(bibles)) {

			for (TBible bible : bibles) {
				TreeBranch bibleBranch = new TreeBranch(bible.getName());
				bibleBranch.setIcon(ResourcesConstants.I_BTV_BIBLE);

				oldBranch = new TreeBranch(oldBLabel);
				oldBranch.setIcon(ResourcesConstants.I_BTV_STAR);
				
				newBranch = new TreeBranch(newBLabel);
				newBranch.setIcon(ResourcesConstants.I_BTV_CROSS);

				if (Mode.OLD_NEW.equals(mode)) {
					bibleBranch.add(oldBranch);
					bibleBranch.add(newBranch);
				}

				if (!CollectionUtils.isEmpty(bible.getBooks())) {
					for (TBook book : bible.getBooks()) {
						TreeBranch toInsert = null;

						switch (mode) {
						case LIST:
							toInsert = bibleBranch;
							break;
						case OLD_NEW:
							toInsert = book.isOldTestament() ? oldBranch
									: newBranch;
							break;
						default:
							break;
						}

						final TreeNode treeNode = new TreeNode(book.getName());
						treeNode.setIcon(ResourcesConstants.I_BTV_BOOK);
						toInsert.insert(treeNode, toInsert.getLength());

					}
				}

				root.add(bibleBranch);
			}
		} else {
			clear();
		}

		setTreeData(root);
	}

	/**
	 * Gets the mode.
	 * 
	 * @return the mode
	 */
	public Mode getMode() {
		return mode;
	}

	/**
	 * Sets the mode.
	 * 
	 * @param mode
	 *            the new mode
	 */
	public void setMode(Mode mode) {
		this.mode = mode;
		refresh();
	}

	/**
	 * Gets the bibles.
	 * 
	 * @return the bibles
	 */
	public List<TBible> getBibles() {
		return bibles;
	}

	/**
	 * Sets the bibles.
	 * 
	 * @param bibles
	 *            the new bibles
	 */
	public void setBibles(List<TBible> bibles) {
		this.bibles = bibles;
		refresh();
	}

	@Override
	public void setI18n(I18n i18n) {
		oldBLabel = i18n.get(this.getClass(), "oldtestament");
		newBLabel = i18n.get(this.getClass(), "newtestament");
		
		if(oldBranch != null){
			oldBranch.setText(oldBLabel);
			newBranch.setText(newBLabel);
//			repaint(true);
		}
	}

}
